package org.assocsp3.web;

import org.assocsp3.modele.Personne;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller
@RequestMapping("/register")
public class RegisterController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(RegisterController.class);

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView initPersonne() {
		logger.info("Register");
		
		ModelAndView m_w = new ModelAndView("register", "personne", new Personne());
		m_w.addObject("personne_valide", false);
		return m_w;  
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(method=RequestMethod.POST)
	public String processForm(@Valid Personne personne, BindingResult result, ModelMap model)
	{
		if(!result.hasErrors()&&personne.memeMdp())
		{
			model.addObject("personne_valide", true);
		}
		else
			model.addObject("mdpErr", "confirmation du mot de passe invalide");
		return "register";
	}
}
